package com.gusti.common.controller.login;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.gusti.common.form.LoginForm;
import com.gusti.common.services.ILoginServices;
import com.gusti.common.vos.Confirmacion;
import com.gusti.common.vos.RolUsuario;
import com.gusti.common.vos.Usuario;

@Controller
@RequestMapping("/login.do")
public class LoginController {

	@Autowired
	private ILoginServices loginServices;
	
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView doLogin( Map<String, Object> model, LoginForm loginForm, HttpSession session ) throws Exception {
		
		Confirmacion confirmacion = loginServices.login( loginForm );
		if( confirmacion.isEstado() ){
			Usuario user = (Usuario) confirmacion.getValueObject();
			session.setAttribute( "user", user );
			session.setMaxInactiveInterval( 3000 );

			if( user.getRolUsuario() == RolUsuario.ADMINISTRADOR )
				model.put( "identifier", "/pelotero/admin/home.do" );
			if( user.getRolUsuario() == RolUsuario.EMPLEADOR )
				model.put( "identifier", "/pelotero/employer/home.do" );
			if( user.getRolUsuario() == RolUsuario.USUARIO )
				model.put( "identifier", "/pelotero/user/home.do" );
		} else {
			model.put( "identifier", "/pelotero/" );
		}
		return new ModelAndView( "identifier", "model", model );
	}

}
